﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using Assistant;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using YoucaiReader.Entities;
using YoucaiReader.Helper;
using YoucaiReader.ViewModels;

namespace YoucaiReader.Pages
{
    public partial class BookChapterPage : PhoneApplicationPage
    {
        private ChapterInfoListViewModel chapterList = new ChapterInfoListViewModel();//目录
        private MarkerInfoListViewModel markerList = new MarkerInfoListViewModel();//书签
        protected PerformanceProgressBar chapterProgressBar = new PerformanceProgressBar();

        public BookChapterPage()
        {
            InitializeComponent();
            AddHeaderToPivotItem();
            base.BackKeyPress += BookChapterPage_BackKeyPress;
        }

        /// <summary>
        /// 添加标题至PivotItem
        /// <para>因为代码中的多个PivotItem只能包含一个phone:PivotItem.Header控件</para>
        /// </summary>
        private void AddHeaderToPivotItem()
        {
            //<TextBlock Foreground="White" FontSize="45" Margin="-15,0,0,0" Text="目录"/>
            Color color = Colors.White;
            double fontsize = 45;
            Thickness thickness = new Thickness(-15, 0, 0, 0);
            this.piChapter.Header = new PivotItemHelper().CreateTextBlock("目录",thickness,fontsize, Colors.White);
            this.piMarket.Header = new PivotItemHelper().CreateTextBlock("书签", thickness, fontsize, Colors.White);
        }

        void BookChapterPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //保证其返回至阅读页
            if (base.NavigationService.CanGoBack)
                base.NavigationService.GoBack();
        }

        protected void AddPerformanceProgressBar()
        {
            if (!this.chapterProgressBar.IsIndeterminate)
            {
                this.chapterProgressBar.IsIndeterminate = true;
                this.chapterProgressBar.Height = 20;
                this.chapterProgressBar.Foreground = new SolidColorBrush(Colors.Red);
            }
        }

        private void lstChapter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressListModel valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressListModel;
                    valueFromSettings.int_ReaderIndex_M = this.chapterList.T_ChapterInfoListViewModel()[listBox.SelectedIndex].ChapterList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
            listBox.SelectedIndex = -1;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            App.QQReader_prePageId = App.QQReader_pageId;
            App.QQReader_pageId = "QQReader_ChapterPage";
            this.LayoutRoot.Children.Add(this.chapterProgressBar);
            this.AddPerformanceProgressBar();
            (new Thread(() => Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (!App.QQReader_StorageFile.IsFile(string.Concat(App.ReaderFileName, "chapterList")))
                {
                    if (!(new ChapterParser(App.ReaderFileName)).Parser())
                    {
                        Prompt.PromptMessage("提示：智能断章错误！");
                    }
                    else
                    {
                        Prompt.PromptMessage("提示：智能断章成功！");
                    }
                }
                this.lstChapter.ItemsSource = this.chapterList.T_ChapterInfoListViewModel();
                this.chapterProgressBar.IsIndeterminate = false;
            }))).Start();
            this.lstMarker.ItemsSource = this.markerList.T_MarkerInfoListViewModel();
        }

        private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void lstMarker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressListModel valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressListModel;
                    valueFromSettings.int_ReaderIndex_M = this.markerList.T_MarkerInfoListViewModel()[listBox.SelectedIndex].MarkerList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
            listBox.SelectedIndex = -1;
        }

        private void lstMarketNavigate(object sender, RoutedEventArgs e)
        {
            BookMarkerListModel bookMarkerListM = new BookMarkerListModel();
            bookMarkerListM = (sender as MenuItem).DataContext as BookMarkerListModel;
            if (bookMarkerListM != null)
            {
                if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
                {
                    LocalBookProgressListModel valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressListModel;
                    valueFromSettings.int_ReaderIndex_M = bookMarkerListM.MarkerList_Index;
                    SettingHelper.SaveInToSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook"), valueFromSettings);
                }
                base.NavigationService.GoBack();
            }
        }

        private void lstMarketRemove(object sender, RoutedEventArgs e)
        {
            BookMarkerListModel bookMarkerListM = new BookMarkerListModel();
            bookMarkerListM = (sender as MenuItem).DataContext as BookMarkerListModel;
            if (bookMarkerListM != null)
            {
                this.markerList.Delete_MarkerInfoList(bookMarkerListM);
            }
        }
    }
}